package com.mts.javacommon.jpabase;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 *
 *
 * <pre>
 *      PageUtil
 * </pre>
 *
 * @author Johnny
 * @date 2021-09-28
 */
@Data
public class PageUtil {

  public static Pageable initPage(PageVo page) {
    Pageable pageable = null;
    int pageNumber = page.getPageNumber();
    int pageSize = page.getPageSize();
    String sort = page.getSort();
    String order = page.getOrder();
    if (pageNumber < 0) {
      pageNumber = 0;
    }
    if (pageSize < 1) {
      pageSize = 10;
    }
    if (StringUtils.isNotEmpty(sort)) {
      Sort.Direction d;
      if (StringUtils.isEmpty(order)) {
        d = Sort.Direction.DESC;
      } else {
        d = Sort.Direction.valueOf(order.toUpperCase());
      }
      Sort s = Sort.by(d, sort);
      pageable = PageRequest.of(pageNumber, pageSize, s);
    } else {
      pageable = PageRequest.of(pageNumber, pageSize);
    }
    return pageable;
  }
}
